package com.ssy.service.impl;

import cn.hutool.core.util.NumberUtil;
import com.ssy.common.CodeMsg;
import com.ssy.common.PageInfo;
import com.ssy.common.Result;
import com.ssy.dao.StudentDao;
import com.ssy.entity.Student;
import com.ssy.entity.User;
import com.ssy.service.StudentService;
import com.ssy.vo.CurriculumVo;
import com.ssy.vo.ScoreVo;
import com.ssy.vo.StudentVo;
import com.sun.xml.internal.bind.v2.model.core.ID;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * StudentServiceImpl
 *
 * @author ssy
 * @date 2020/11/16
 **/
public class StudentServiceImpl implements StudentService {

    StudentDao studentDao = new StudentDao();

    @Override
    public Result selectScoreInfo(String number) {
        Student student = this.studentDao.selectClassId(number);
        List<ScoreVo> scoreVos = this.studentDao.queryScoreInfo(student.getsId());
        PageInfo<ScoreVo> pageInfo = new PageInfo<>();
        pageInfo.setCount(scoreVos.size());
        pageInfo.setLimit(Integer.MAX_VALUE);
        pageInfo.setPage(1);
        pageInfo.setList(scoreVos);
        return new Result(pageInfo);
    }

    @Override
    public Result selectcurriculumInfo(String number) {
        Student student = this.studentDao.selectClassId(number);
        List<CurriculumVo> curriculumVo = this.studentDao.querycurriculumInfo(student.getSclass());
        PageInfo<CurriculumVo> pageInfo = new PageInfo<>();
        pageInfo.setCount(curriculumVo.size());
        pageInfo.setLimit(Integer.MAX_VALUE);
        pageInfo.setPage(1);
        pageInfo.setList(curriculumVo);
        return new Result(pageInfo);
    }

    @Override
    public Result selectStudentInfo(String number) {
        StudentVo studentVo = this.studentDao.queryStudentInfo(number);
        PageInfo<StudentVo> pageInfo = new PageInfo<>();
        pageInfo.setCount(1);
        pageInfo.setLimit(Integer.MAX_VALUE);
        ArrayList<StudentVo> studentVos = new ArrayList<>();
        studentVos.add(studentVo);
        pageInfo.setList(studentVos);
        pageInfo.setPage(1);
        return new Result(pageInfo);
    }

    @Override
    public Student selectStudentByStudentNum(String studentNumberId) {
        return this.studentDao.queryStudentByStudentNum(studentNumberId);
    }

    @Override
    public Result deleteOneStudent(String id) {
        //删除成绩表
        Integer[] idNo = {Integer.parseInt(id)};
        this.studentDao.delScore(idNo);
        //删除学生表
        boolean flag = this.studentDao.deleteStudentById(Integer.parseInt(id));
        return flag ? new Result(CodeMsg.DELETE_SUCCES) : new Result(CodeMsg.DELETE_ERROR);
    }

    @Override
    public Result deteleUser(String[] ids) {
        //删除成绩表
        this.studentDao.bathScore(ids);

        boolean flag = this.studentDao.batchDeleteUser(ids);
        return flag ? new Result(CodeMsg.DELETE_SUCCES) : new Result(CodeMsg.DELETE_ERROR);
    }

    @Override
    public Result updateStudent(String sId, String number, String sname, String major2, String className, String sex, String birthday, String address, String phone, String remarks) {
        Student student1 = this.studentDao.selectStudentBySid(sId);

        //学号不相等
        if (!student1.getNumber().equals(number)) {
            Student student = this.studentDao.queryStudentByStudentNum(number);
            if (student != null) {
                return new Result(CodeMsg.STUDENT_NUMBER_ALREADY_EXISTS);
            }
        }
        //电话号码不相等
        if (!student1.getPhone().equals(phone)) {
            Student student = this.studentDao.selectStudentByPhone(phone);
            if (student != null) {
                return new Result(CodeMsg.STUDENT_PHONE_ALREADY_EXISTS);
            }
        }

        boolean flag = this.studentDao.updateStudent2(Integer.parseInt(sId), number, sname, sex, birthday, Integer.parseInt(major2), Integer.parseInt(className), address, phone, remarks);
        return flag ? new Result(CodeMsg.UPDATE_SUCCES) : new Result(CodeMsg.UPDATE_ERROR);

    }

    @Override
    public Result addStudent(String number, String sname, String major2, String className, String sex, String birthday, String address, String phone, String remarks) {
        Student student = this.studentDao.queryStudentByStudentNum(number);
        if (student != null) {
            return new Result(CodeMsg.STUDENT_NUMBER_ALREADY_EXISTS);
        }

        Student student2 = this.studentDao.selectStudentByPhone(phone);
        if (student2 != null) {
            return new Result(CodeMsg.STUDENT_PHONE_ALREADY_EXISTS);
        }

        boolean flag = this.studentDao.insertStudent(number, sname, sex, birthday, Integer.parseInt(major2), Integer.parseInt(className), address, phone, remarks);
        return flag ? new Result(CodeMsg.ADD_SUCCES) : new Result(CodeMsg.ADD_ERROR);
    }

    @Override
    public Result queryPage(HashMap<String, Object> param, String page, String limit) {
        Integer pageNo = NumberUtil.isNumber(page) ? Integer.parseInt(page) : 1;
        Integer limitNo = NumberUtil.isNumber(limit) ? Integer.parseInt(limit) : 10;
        PageInfo<StudentVo> userPageInfo = this.studentDao.selectPage(param, pageNo, limitNo);
        return new Result(userPageInfo);
    }

    @Override
    public Result queryClassNameList() {
        return this.studentDao.selectAllClassName();
    }

    @Override
    public Result queryMajorList() {
        return this.studentDao.selectAllMajor();
    }
}
